import { Component, OnInit } from '@angular/core';
import { ApiService } from './../../../../core/api/api.service';
import { FormBuilder, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { MessageService } from './../../../../core/service/message.service';

@Component({
    templateUrl: './personnel.html',
    styles: [
        `
          [nz-button] {
            margin-right: 8px;
            margin-bottom: 12px;
          }
        `
      ]
})
export class PersonnelComponent implements OnInit {
    page = {pageNo: 1};
    personnelList = [];
    departmentList = [];
    staffTypeList = [];
    proTitleList = [];
    departmentTypes = {};
    selectedRowData: any = {};
    departmentTypesList = [];
    form: any = {};
    isVisible = false;
    isVisible2 = false;
    editTable = false;
    validateForm;
    url;
    constructor(
        private apiService: ApiService,
        private fb: FormBuilder,
        private message: NzMessageService,
        private msg: MessageService
        ) {
    }
    ngOnInit() {
        this.url = this.apiService.environment().medical;
        console.log(this.url);
        this.msg.setTitle('人员维护');
        this.loadList();
        this.validateForm = this.fb.group({
            staffNo: [null, [Validators.required]],
            contactName: [null, [Validators.required]],
            contactMobile: [null, [Validators.required, Validators.pattern(/^1[0-9]{10}$/)]],
            contactEmail: [null, [Validators.required, Validators.email]],
            certId: [null, [Validators.required]],
            departmentId: [null, [Validators.required]],
            staffTypeId: [null, [Validators.required]],
            proTitleId: [null, [Validators.required]],
            memo: [null ],
            useStatus: [null ],
        });
        this.apiService.post('医院科室列表', {...this.page}).then((res: any) => {
            this.departmentList = res.data.list;
        });
        // this.apiService.post('医院职工类别集合和医院职称名称集合',{})
    }
    loadList() {
        this.apiService.post('医院人员维护列表', {...this.page}).then((res: any) => {
            this.personnelList = res.data.list;
        });
    }
    changeDepartment(e){
        if(e){
            const a = this.departmentList.filter(i => i.id === e);
            this.form.departmentTypeId = a[0].departmentTypeId;
            this.proTitleList = [];
            this.validateForm.controls.staffTypeId.setValue(null);
            this.validateForm.controls.proTitleId.setValue(null);
            this.apiService.post('医院职工类别集合', {departmentTypeId: this.form.departmentTypeId}).then((res: any) => {
                this.staffTypeList = res.data;
            });
        }
    }
    changestaffType(e) {
        this.validateForm.controls.proTitleId.setValue(null);
        if (e) {
            this.apiService.post('医院职称集合', {departmentTypeId: this.form.departmentTypeId,staffTypeId: e}).then((res: any) => {
                this.proTitleList = res.data;
                console.log(this.validateForm);
            });
        }
    }
    updateTable() {
        this.editTable = !this.editTable;
    }
    handleCancel() {
        this.isVisible = false;
    }
    handleOk() {
        for (const key in this.validateForm.controls) {
            this.validateForm.controls[key].markAsDirty();
            this.validateForm.controls[key].updateValueAndValidity();
        }
        if (this.validateForm.valid) {
            const url = this.selectedRowData.id ? '编辑医院职工' : '创建医院职工';
            const data = this.selectedRowData.id ?
            {...this.validateForm.value, id: this.selectedRowData.id } :
            {...this.validateForm.value, hospitalCode: '', hospitalName: '',  ...this.form };
            this.apiService.post(url, data).then((res: any) => {
                const message = this.selectedRowData.id ? '编辑职工成功！' : '创建职工成功！';
                this.isVisible = false;
                this.validateForm.reset();
                this.message.success(message);
                this.loadList();
            });
        }
    }
    add() {
        this.selectedRowData = {};
        this.validateForm.reset();
        this.validateForm.controls.staffNo.enable();
        this.validateForm.controls.contactName.enable();
        this.validateForm.controls.contactMobile.enable();
        this.validateForm.controls.contactEmail.enable();
        this.validateForm.controls.certId.enable();
        this.validateForm.controls.certId.setValidators([Validators.required]);
        this.validateForm.controls.departmentId.enable();
        this.validateForm.controls.useStatus.disable();
        this.isVisible = true;
    }
    edit() {
        if (!this.selectedRowData.id) {
            this.message.warning('请选择一条数据！');
            return;
        }
        this.validateForm.controls.staffNo.disable();
        this.validateForm.controls.contactName.disable();
        this.validateForm.controls.contactMobile.disable();
        this.validateForm.controls.contactEmail.disable();
        this.validateForm.controls.certId.disable();
        this.validateForm.controls.certId.setValidators([]);
        this.validateForm.controls.departmentId.disable();
        this.validateForm.controls.useStatus.enable();
        this.validateForm.patchValue(this.selectedRowData);
        this.isVisible = true;
    }
    copy() {
        if (!this.selectedRowData.id) {
            this.message.warning('请选择一条数据！');
            return;
        }
        if(this.selectedRowData.regStatus !== 'CREATE'){
            this.message.warning('当前职工已申请注册！');
            return;
        }
        this.isVisible2 = true;
    }
    selectedRow(data){
        this.selectedRowData = data;
    }
}
